﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SqlServerDataTableToExcel.DBhelper
{
  public  class SqlServerDBHelper
    {

      private string connStr { get; set; }

      public  SqlServerDBHelper(string IPAddress, string DataSource, string UserName, string Pwd)
        {
            if (string.IsNullOrEmpty(IPAddress))
            {
                throw new Exception("服务器IP地址不能为空！");
            }
            connStr= string.Format("server={0};database={1};uid={2};pwd={3}", IPAddress, DataSource, UserName,Pwd);
        }


        public  DataTable GetUsrDataSource(string sql)
        {
            if (string.IsNullOrWhiteSpace(connStr))
            {
                return null;
            }
            SqlConnection conn = new SqlConnection(connStr);
            SqlDataAdapter da = new SqlDataAdapter(sql, conn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }
    }
}
